package com.bridgeintelligent.tag.user.query.service;

import com.bridgeintelligent.tag.user.mgmt.entity.Result;
import com.bridgeintelligent.tag.user.mgmt.entity.UserCodes;
import com.bridgeintelligent.tag.user.mgmt.pojo.Power;
import com.bridgeintelligent.tag.user.mgmt.pojo.Role;
import com.bridgeintelligent.tag.user.mgmt.pojo.TagPermission;
import com.bridgeintelligent.tag.user.query.entity.RolePage;
import com.bridgeintelligent.tag.user.query.mapper.PowerQueryMapper;
import com.bridgeintelligent.tag.user.query.mapper.RoleQueryMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 角色服务层接口实现
 */

@Service
public class RoleQueryServiceImpl implements RoleQueryService {

    @Autowired
    private RoleQueryMapper roleQueryMapper;
    @Autowired
    private PowerQueryMapper powerQueryMapper;


    /**
     * 校验角色名是否重复
     *
     * @param
     * @return
     */
    @Override
    public boolean checkRoleName(String roleName) {
        int count = roleQueryMapper.checkRoleName(roleName);
        if (count == 1) {
            return false;
        }
        return true;
    }

    /**
     * 根据角色id查询角色名
     *
     * @param roleId
     * @return
     */
    @Override
    public String findRoleNameById(String roleId) {
        String roleName = roleQueryMapper.findRoleNameById(roleId);
        return roleName;
    }

    /**
     * 根据角色id查询单个角色信息及其权限信息
     *
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findOneByRoleId(String roleId) {
        //角色信息
        Role role = roleQueryMapper.findOneByRoleId(roleId);
        if (role == null) {
            return null;
        }
        if (UserCodes.ISLOCKED_01.equals(role.getIsLocked())) {
            role.setIsLockedName("锁定");
        } else if (UserCodes.ISLOCKED_02.equals(role.getIsLocked())) {
            role.setIsLockedName("未锁定");
        }
        if (UserCodes.EFFTCTIVE_SYMBOL_01.equals(role.getEfftctiveSymbol())) {
            role.setIsEfftctive("有效");
        } else if (UserCodes.EFFTCTIVE_SYMBOL_02.equals(role.getEfftctiveSymbol())) {
            role.setIsEfftctive("无效");
        }
        //根据角色id查询其所有权限id集合
        List<String> powerIds = findPowerByRoleId(roleId);
        List<TagPermission> tagPerms = findTagPermsByRoleId(roleId);
        //设置权限信息
        role.setPowerIds(powerIds);
        role.setTagPerms(tagPerms);

        //查询所有权限信息集合
        List<Power> powerList = findAllPower(UserCodes.POWERPARENTID);
        Map<String, Object> map = new HashMap<>();
        map.put("role", role);
        map.put("powerList", powerList);
        return map;
    }

    /**
     * 根据角色id查询其权限id
     *
     * @param roleId
     * @return
     */
    @Override
    public List<String> findPowerByRoleId(String roleId) {
        //根据角色id查询其所有权限
        return roleQueryMapper.findPowerByRoleId(roleId);
    }

    /**
     * 根据角色id查询其标签权限
     *
     * @param roleId
     * @return
     */
    @Override
    public List<TagPermission> findTagPermsByRoleId(String roleId) {
        //根据角色id查询其所有权限
        return roleQueryMapper.findTagPermsByRoleId(roleId);
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> findAllRole() {
        List<Role> roleList = roleQueryMapper.findAllRole();
        if (roleList == null) {
            return null;
        }
        for (Role role : roleList) {
            if (UserCodes.ISLOCKED_01.equals(role.getIsLocked())) {
                role.setIsLockedName("锁定");
            } else if (UserCodes.ISLOCKED_02.equals(role.getIsLocked())) {
                role.setIsLockedName("未锁定");
            }
            if (UserCodes.EFFTCTIVE_SYMBOL_01.equals(role.getEfftctiveSymbol())) {
                role.setIsEfftctive("有效");
            } else if (UserCodes.EFFTCTIVE_SYMBOL_02.equals(role.getEfftctiveSymbol())) {
                role.setIsEfftctive("无效");
            }
        }
        return roleList;
    }

    /**
     * 分页条件查询
     *
     * @param pageNumber
     * @param pageSize
     * @param roleName
     * @return
     */
    @Override
    public Result list(Integer pageNumber, Integer pageSize, String roleName) {
        Page<List<Role>> page = PageHelper.startPage(pageNumber, pageSize);
        List<Role> list = roleQueryMapper.findList(roleName);
        if (list == null) {
            return null;
        }
        for (Role role : list) {
            if (UserCodes.ISLOCKED_01.equals(role.getIsLocked())) {
                role.setIsLockedName("锁定");
            } else if (UserCodes.ISLOCKED_02.equals(role.getIsLocked())) {
                role.setIsLockedName("未锁定");
            }
            if (UserCodes.EFFTCTIVE_SYMBOL_01.equals(role.getEfftctiveSymbol())) {
                role.setIsEfftctive("有效");
            } else if (UserCodes.EFFTCTIVE_SYMBOL_02.equals(role.getEfftctiveSymbol())) {
                role.setIsEfftctive("无效");
            }
        }

        RolePage<Role> rolePage = new RolePage<>();
        rolePage.setContent(list);
        rolePage.setTotalPages(page.getPages());
        rolePage.setTotalElements(page.getTotal());
        rolePage.setSize(page.getPageSize());
        rolePage.setRequestPager(pageNumber);
        return new Result(UserCodes.SUCCESS, rolePage);

    }

    /**
     * 查询所有权限列表
     *
     * @param s
     * @return
     */
    @Override
    public List<Power> findAllPower(String s) {
        List<Power> powerList = powerQueryMapper.findAllPower(s);
        if (powerList != null && powerList.size() > 0) {
            for (Power power : powerList) {
                List<Power> list = findAllPower(power.getPowerId());
                if (list != null && list.size() > 0) {
                    power.setPowerList(list);
                }
            }
        }
        return powerList;
    }

    @Override
    public String findRoleIdByName(String roleName) {
        if(StringUtils.isBlank(roleName)){
            return null;
        }
        List<Role> roles = roleQueryMapper.findByName(roleName.trim());
        if(CollectionUtils.isEmpty(roles)){
            return null;
        }
        return roles.get(0).getRoleId();
    }

}
